Table of Contents

Module: Callbacks .\src\TW\Callbacks.py

Callback Objects Using Weak References

A common problem in implementing the "Observer" pattern in Python is that containers often want to be an observer of their contents. This makes it tricky to pass callback information to the observed object without creating a circular reference. In limited applications of the observer pattern with relatively static observer-subject relationships, one can code around this, but it would be much nicer if you could just pass a callback object to anything that needed "subscribing" to, and not have to worry about the created references.

The TW.Callbacks module defines a simple interface, ICallback, and provides a Callback class which implements it. It also provides a CallbackList class which manages a set of subscribed ICallback implementations.

The supplied implementations address a variety of low-level issues such as handling callbacks to objects which do not support weak references (in which case a normal reference is used). There is also an experimental Dispatcher class which is intended to support event keys, with each callback capable of being subscribed to more than one event at once, but receiving only one call even if multiple simultaneous events are fired.

Quick usage overview:

        from TW.Callbacks import Callback, CallbackList

        class someSubject:

            def __init__(self):
                self.observers = CallbackList()
                self.subscribe = self.observers.subscribe
                self.unsubscribe = self.observers.remove

        class anObserver:
            def __init__(self, aSubject):
                aSubject.subscribe( Callback(self,'myCallbackMethod') )

See the classes and source code for more details.

Imported modules   
import Interface
from UserList import UserList
from weakref import ref
Classes   
Callback

Encapsulate a callback with support for weak references

CallbackList

A list of callbacks that can be called all at once

Dispatcher

Dispatches events by event keys

ICallback

Encapsulate a callback with support for weak references


Table of Contents

This document was automatically generated on Tue Mar 05 10:33:43 2002 by HappyDoc version WORKING